package com.woniuxy.woniuticket.movieinfomodule.dao;

import com.woniuxy.woniuticket.pojo.CinemaVip;
import com.woniuxy.woniuticket.pojo.UserVipInfo;
import com.woniuxy.woniuticket.movieinfomodule.dto.LoginUserAndVip;
import com.woniuxy.woniuticket.movieinfomodule.dto.UserandVip;
import org.apache.ibatis.annotations.*;
import org.elasticsearch.search.DocValueFormat;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Mapper
public interface UserVipInfoDAO {
    //插入数据
    //insert INTO t_users_vip VALUES(1,2,'2019-06-20 15:53:36',DATE_ADD('2019-06-20 15:53:36', INTERVAL 1 year))
    @Insert("insert into t_users_vip values(#{userId},#{vipId},#{startDate},DATE_ADD(#{startDate}, INTERVAL 1 year))")
    void insertUserVipInfo(UserVipInfo userVipInfo);

    //通过用户id删除用户信息
    @Delete("delete from t_users_vip where userId = #{userId}")
    void deleUserVipInfo(Integer userId);

    //vip过期后删除信息
    @Delete("delete from t_users_vip where endDate < #{date}")
    void deledeleUserVipInfoForDateout(Date date);

    //查询vip到期还有小于等于5天的用户
    @Select("select * from t_users_vip where endDate <= DATE_ADD(#{date}, INTERVAL 5 day)")
    List<UserVipInfo> queryUserVipInfo(Date date);

    //通过vipid查询用户，当前使用人数
    @Select("select count(*) from t_users_vip where vipId = #{vipId}")
    int queryUserNum(Integer vipId);

    //查询用户所有信息
    @Select("select * from t_users_vip v,t_users_info u ,t_cinema_vip c where u.id=v.userId and c.id = v.vipId")
    List<UserandVip> queryAll();

    //查询用户所有信息加搜索条件

    List<UserandVip> queryAll1(UserandVip userandVip);
    /*@Select({

       "<if text='all.startDate != null'>" ,
            "and  v.endDate >![CDATA[<=]] #{all.startDate}",
            "</if>",
            "<if text='all.endDate != null'>" ,
            "and v.endDate <![CDATA[<=]] #{all.endDate}",
            "</if>",


            "<script>",
            "select e.*,d.deptno 'dept.deptno' ,d.dname 'dept.dname' from emp e left join dept d on e.deptno = d.deptno",
            "<where>",
            "<if text='ve.ename != null and  !=ve.ename'>" ,
            "and e.ename like concat('%',#{ve.ename},'%')",
            "</if>",
            "<if text='ve.begin != null'>" ,
            "and e.hiredate <![CDATA[>=]] #{ve.begin}",
            "</if>",
            "<if text='ve.end != null'>" ,
            "and e.hiredate <![CDATA[<=]] #{ve.end}",
            "</if>",
            "</where>",
            "</script>"
    })*/

    //根据登录用户账号查询所有相关信息
    @Select("select * from t_users_vip v,t_users_info u ,t_cinema_vip c,t_users_login log"+
            "where u.id=v.userId and c.id = v.vipId and log.user_info_id = u.id and  log.account = #{account}")
    LoginUserAndVip queryAllForLogin(String account);

    //查询单个用户VIP信息
    @Select("select * from t_users_vip  where userId = #{userId}")
    UserVipInfo querySingleVipUser(Integer userId);

    //根据用户info的 id获取所办理vip的折扣
    @Select("select c.vdiscount from t_users_vip v ,t_cinema_vip c where v.vipId = c.id and v.userId =#{userId}")
    BigDecimal  queryVipById(Integer userId);

    //用来做用户信息展示
    @Select("select * from t_users_vip v,t_users_info u ,t_cinema_vip c where u.id=v.userId and c.id = v.vipId and telnumber=#{telnumber}")
    UserandVip queryUserInfo(String telnumber);


}
